﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace CMS_GYM.Interface
{
    public partial class frmChucNangChuongTrinh : frmTemplete1
    {
        #region "Initials"
        #region frmModules()
        public frmChucNangChuongTrinh()
        {
            InitializeComponent();
            lblTitle.Text = "CHỨC NĂNG CHƯƠNG TRÌNH";
            uct_Refresh.Refresh.Click += new EventHandler(btn_Refresh_Click);
            uct_Save.Save.Click += new EventHandler(btn_Save_Click);
            LoadData(true);
        }
        #endregion
        #endregion

        #region "Functions"
        
        private void SetPermission()
        {
            //uct_Reset.Enabled = clsUser.LayThongTinPhanQuyen("BangGiaCauLong").ChucNang.Them;
            uct_Save.Enabled = clsUser.LayThongTinPhanQuyen("ChucNang").ChucNang.Sua;
            //uct_Delete.Enabled = clsUser.LayThongTinPhanQuyen("BangGiaCauLong").ChucNang.Xoa;
        }

        #region LoadData(bool isReadOnly)
        public void LoadData(bool isReadOnly)
        {
            try
            {
                LayBangChucNangChuongTrinh();
                dtg_Data.AllowUserToAddRows = !isReadOnly;
                dtg_Data.AllowUserToDeleteRows = !isReadOnly;
                dtg_Data.ReadOnly = isReadOnly;
                uct_Save.Enabled = !isReadOnly;
                dtg_Data_Resize(null, null);
            }
            catch { }
        }
        #endregion

        #region LayBangChucNangChuongTrinh()
        private void LayBangChucNangChuongTrinh()
        {
            try
            {
                dtg_Data.Rows.Clear();
                clsChucNangBUS ChucNangBus = new clsChucNangBUS();
                DataTable dt = ChucNangBus.LayBang();
                DataView dv = new DataView(dt);
                dv.Sort = "TenChucNang";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRowView drv = dv[i];
                    dtg_Data.Rows.Add(new object[] { drv["MaChucNang"], drv["TenChucNang"] });
                }
            }
            catch { }
        }
        #endregion
        #endregion

        #region "Events"
        #region btn_Refresh_Click(object sender, EventArgs e)
        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            LayBangChucNangChuongTrinh();
        }
        #endregion

        #region btn_Save_Click(object sender, EventArgs e)
        private void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                string strXml = "<Root>";
                for (int i = 0; i < dtg_Data.Rows.Count - 1; i++)
                {
                    DataGridViewRow row = dtg_Data.Rows[i];
                    int moduleID = 0;
                    string moduleName = string.Empty, controlName = string.Empty;
                    if (row.Cells["ModuleID"].Value != null)
                        moduleID = int.Parse(row.Cells["ModuleID"].Value.ToString());
                    if (row.Cells["ModuleName"].Value != null)
                        moduleName = row.Cells["ModuleName"].Value.ToString();
                    if (row.Cells["ControlName"].Value != null)
                        controlName = row.Cells["ControlName"].Value.ToString();
                    //strXml += "<Modules ModuleID = \"" + Common.Functions.RefreshXmlString(moduleID.ToString())
                    //    + "\" ModuleName = \"" + Common.Functions.RefreshXmlString(moduleName)
                    //    + "\" ControlName = \"" + Common.Functions.RefreshXmlString(controlName)
                    //    + "\"/>";
                }
                strXml += "</Root>";
                //int result = BLModules.Save(strXml);
                int result = 0;
                //PscMessage.ShowResult(result, "Lưu danh sách chức năng");
                if (result == 0) LayBangChucNangChuongTrinh();
            }
            catch (Exception ex)
            {
                //PscMessage.Show(ex.Message, "ERROR", ListButton.Close, ICon.Error);
            }
        }
        #endregion

        #region dtg_Data_Resize(object sender, EventArgs e)
        private void dtg_Data_Resize(object sender, EventArgs e)
        {
            try
            {
                ModuleName.Width = dtg_Data.Width - ((dtg_Data.Width - 90) / 3) - 90;
                ControlName.Width = (dtg_Data.Width - 90) / 3;
                Delete.Width = 60;
            }
            catch { }
        }
        #endregion

        private void frmChucNangChuongTrinh_Load(object sender, EventArgs e)
        {
            try
            {
                SetPermission();
            }
            catch { }
        }
        #endregion
    }
}